package k8s_ui

import (
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"k8s-platform/service/k8s-ui"
	"net/http"
)

// 获取configmap列表，支持过滤、排序、分页
var K8s_ConfigMap k8s_configmap

type k8s_configmap struct{}

// 查询configmap
func (*k8s_configmap) GetConfigMap(ctx *gin.Context) {
	params := new(struct {
		FilterName string `form:"configmap_name"`
		Namespace  string `form:"namespace"`
		Page       int    `form:"page"`
		Limit      int    `form:"limit"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	data, err := k8s_ui.K8s_ConfigMap.GetConfigMaps(params.FilterName, params.Namespace, params.Limit, params.Page)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取configmap列表成功",
		"data": data,
	})
}

// 获取configmap详情
func (d *k8s_configmap) GetConfigMapDetail(ctx *gin.Context) {
	params := new(struct {
		FilterName string `form:"configmap_name"`
		Namespace  string `form:"namespace"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	data, err := k8s_ui.K8s_ConfigMap.GetConfigMapDetail(params.FilterName, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取ConfigMap详情成功",
		"data": data,
	})
}

// 创建configmap
func (d *k8s_configmap) CreateConfigMap(ctx *gin.Context) {
	var (
		configmapCreate = new(k8s_ui.ConfigMapCreate)
		err             error
	)
	if err = ctx.ShouldBindJSON(configmapCreate); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	if err = k8s_ui.K8s_ConfigMap.CreateConfigMap(configmapCreate); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建ConfigMap成功",
		"data": nil,
	})
}

// 删除configmap
func (d *k8s_configmap) DeleteConfigMap(ctx *gin.Context) {
	params := new(struct {
		FilterName string `json:"configmap_name"`
		Namespace  string `json:"namespace"`
	})
	//DELETE请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	err := k8s_ui.K8s_ConfigMap.DeleteConfigMap(params.FilterName,
		params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "删除ConfigMap成功",
		"data": nil,
	})
}

// 更新configmap
func (d *k8s_configmap) UpdateConfigMap(ctx *gin.Context) {
	params := new(struct {
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	err := k8s_ui.K8s_ConfigMap.UpdateConfigMap(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新UpdateConfigMap成功",
		"data": nil,
	})
}

// 获取每个namespace的configmap数量
func (d *k8s_configmap) GetConfigMapNumPerNp(ctx *gin.Context) {
	data, err := k8s_ui.K8s_ConfigMap.GetConfigMapNumPerNp()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取每个namespace的configmap数量成功",
		"data": data,
	})
}
